package cn.cxj.user.controller;

import cn.cxj.basic.exception.BusinessException;
import cn.cxj.basic.util.AjaxResult;
import cn.cxj.basic.util.LogAnnotation;
import cn.cxj.user.dto.LoginDto;
import cn.cxj.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    @LogAnnotation(operateType = "账号密码登陆")
    public AjaxResult accountLogin(@RequestBody LoginDto dto){
        try {
            Map<String, Object> map = loginService.accountLogin(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    @PostMapping("/wechat")
    @LogAnnotation(operateType = "微信登陆")
    public AjaxResult wechatLogin(@RequestBody Map<String,String> map){
        try {
            System.out.println(map);
            return loginService.wechat((map.get("code")));
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    @PostMapping("/binder/wechat")
    @LogAnnotation(operateType = "微信绑定账号")
    public AjaxResult binder(@RequestBody Map<String,String> params){
        try {
            System.out.println(params);
            return loginService.binder(params);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }
}
